const service = require('../service/label_service')

const verifyLabel = async(ctx,next)=>{
  // 取出要添加的标签
  const {labels} = ctx.request.body
  // 判断标签是否已存在
  const newLabels = []
  for(let name of labels){
    const labelResult = await service.getLabelName(name)
    const label = {name}
    if(!labelResult){
      const result = await service.create(name)
      label.id = result.insertId
    }else{
      label.id = labelResult.id
    }
    newLabels.push(label)
  }
  ctx.labels = newLabels

  await next()
}

module.exports = {verifyLabel}